package com.aishua.aishuawallet.adapter;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.aishua.aishuawallet.Constant;
import com.aishua.aishuawallet.R;
import com.aishua.aishuawallet.activity.BaseActivity;
import com.aishua.aishuawallet.web3jmanager.Web3jManager;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * Created by aishua on 2018/4/3.
 */

public class CurrencuyAdapter extends RecyclerView.Adapter<CurrencuyAdapter.MyDeviceViewHoler> {

    private Context mContext;
    private List deviceinfoList;
    private Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            double balace1 = (double) msg.obj;

            tv_xeth.setText(String.format("%.1f",Double.valueOf(balace1)));
            tv_other.setText("(¥)≈"+String.format("%.1f",Double.valueOf(balace1) *1900));
        }
    };
    public CurrencuyAdapter(Context mContext, ArrayList deviceinfoList) {
        this.mContext = mContext;
        this.deviceinfoList = deviceinfoList;
    }

    @Override
    public MyDeviceViewHoler onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(mContext).inflate(R.layout.view_currencuy, parent, false);

        return new MyDeviceViewHoler(view);

    }

    @Override
    public void onBindViewHolder(MyDeviceViewHoler holder, final int position) {

        holder.displayView(position);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                onRecycleViewClickListener.onRecycleViewClick(position);
            }
        });

    }

    @Override
    public int getItemCount() {
        if (null == deviceinfoList || deviceinfoList.size() == 0) {

            return 0;
        }
        return deviceinfoList.size();

//        return  3;
    }
    private TextView tv_xeth;
    private TextView tv_other;
    public class MyDeviceViewHoler extends RecyclerView.ViewHolder {

        private ImageView img_eth;
        private TextView tv_eth;


        public MyDeviceViewHoler(View itemView) {
            super(itemView);
            img_eth = itemView.findViewById(R.id.img_eth);
            tv_eth = itemView.findViewById(R.id.tv_eth);
            tv_xeth = itemView.findViewById(R.id.tv_xeth);
            tv_other = itemView.findViewById(R.id.tv_other);
        }

        public void displayView(int position) {

//        String bock_id = deviceinfoList.get(position).getIotId();
//        String imei = deviceinfoList.get(position).getImei();

            new Thread(){
                @Override
                public void run() {

                    double balace = getBalace(Constant.ADDRESS_OWN) ;
                    Message message = mHandler.obtainMessage();
                    message.what =1;
                    message.obj = balace;
                    mHandler.sendMessage(message);

                }
            }.start();
        }

    }


    public void setonRecycleViewClickListener(onRecycleViewClickListener onRccycleViewLongClickListener) {
        this.onRecycleViewClickListener = onRccycleViewLongClickListener;
    }

    public void setOnRccycleViewLongClickListener(onRccycleViewLongClickListener onRccycleViewLongClickListener) {

        this.onRccycleViewLongClickListener = onRccycleViewLongClickListener;
    }

    private onRecycleViewClickListener onRecycleViewClickListener;
    private onRccycleViewLongClickListener onRccycleViewLongClickListener;

    public interface onRecycleViewClickListener {

        public void onRecycleViewClick(int position);
    }

    public interface onRccycleViewLongClickListener {

        public void onRccycleViewLongClick();
    }


    public double  getBalace(String address){

        double  balace = 0;
        Web3jManager web3jManager = Web3jManager.getIntance();
        try {

            balace =web3jManager.getBalance(address);


        } catch (ExecutionException e) {

            e.printStackTrace();
        } catch (InterruptedException e) {

            e.printStackTrace();
        }
        return balace;

    }
}